import { request } from "@/api";

// 公共接口路径常量
const API_PATHS = {
  EXAM_ANSWER: '/user/exam-answer',
  EXAM_ANALYSIS: '/user/exam-analysis',
  EXAM_RECORD: '/user/exam-record',
  EXAN: '/user/exam',
  EXAM_TOTAL_ANALYSIS: '/user/exam-total-analysis'
};

function getTotalAnalysisByExamId(examId) {
    return request('get', `${API_PATHS.EXAM_TOTAL_ANALYSIS}/get`, examId)
}

function updateExam(exam) {
    return request('post', `${API_PATHS.EXAN}/update`, exam)
}

function analysisThisExam(exam) {
    return request('get', `${API_PATHS.EXAN}/total-analysis`, exam)
}



/*  
* 用户答题记录接口
​**​*​**​*​**​*​**​*​**​*​**​*​**​*​**​*​**​*​**​*​**​*​**​*​**​**/

/*
* 提交单题答案
* 参数data需包含：
*   recordId: 考试记录ID
*   questionId: 试卷题目ID  
*   answer: 用户答案
*/
function submitAnswer(data) {
    return request('post', `${API_PATHS.EXAM_ANSWER}/answer`, data);
}

/*
* 批量提交答案  
* 参数answers为数组，每个元素需包含：
*   recordId: 考试记录ID
*   questionId: 试卷题目ID
*   answer: 用户答案
*/
function batchSubmitAnswers(answers) {
    return request('post', `${API_PATHS.EXAM_ANSWER}/batch-answer`, answers);
}

/*
* 根据ID查询答题记录
* 参数answerId: 答题记录ID
*/
function getAnswerById(answerId) {
    return request('get', `${API_PATHS.EXAM_ANSWER}/answer-id`, { answerId });
}

/*
* 查询用户某次考试的答题记录
* 参数recordId: 考试记录ID
*/
function getAnswersByExamRecord(recordId) {
    return request('get', `${API_PATHS.EXAM_ANSWER}/answer-record-id-user-id`, { recordId });
}

/*
* 更新答题记录（用于批改）
* 参数data需包含：
*   id: 记录ID
*   answer: 用户答案
*   isCorrect: 是否正确
*   score: 得分
*/
function updateAnswer(data) {
    return request('post', `${API_PATHS.EXAM_ANSWER}/update-answer`, data);
}

/*
* 删除单条答题记录
* 参数answerId: 答题记录ID
*/
function deleteAnswer(answerId) {
    return request('post', `${API_PATHS.EXAM_ANSWER}/delete-answer`, { answerId });
}

/*
* 删除某次考试的全部答题记录
* 参数recordId: 考试记录ID
*/
function deleteAnswersByRecord(recordId) {
    return request('post', `${API_PATHS.EXAM_ANSWER}/delete-record-answer`, { recordId });
}

/*
* 结束考试（系统调用）
* 参数examId: 考试ID
*/
function finishExam(examId) {
    return request('post', `${API_PATHS.EXAM_ANSWER}/exam-end`, { examId });
}

/*
* 用户提交考试
* 参数recordId: 考试记录ID
*/
function submitExam(recordId) {
    return request('post', `${API_PATHS.EXAM_ANSWER}/submit-exam`, { recordId });
}

/*
* 考试分析接口
​**​*​**​*​**​*​**​*​**​*​**​*​**​*​**​*​**​*​**​*​**​*​**​*​**​**/

/*
* 创建考试分析
* 参数analysisData: 考试分析数据对象
*/
function createAnalysis(analysisData) {
    return request('post', `${API_PATHS.EXAM_ANALYSIS}/create`, analysisData);
}

/*
* 批量创建考试分析
* 参数analyses: 考试分析数据数组
*/
function batchCreateAnalyses(analyses) {
    return request('post', `${API_PATHS.EXAM_ANALYSIS}/batch-create`, analyses);
}

/*
* 更新考试分析
* 参数analysisData: 更新后的分析数据
*/
function updateAnalysis(analysisData) {
    return request('post', `${API_PATHS.EXAM_ANALYSIS}/update`, analysisData);
}

/*
* 删除考试分析（物理删除）
* 参数analysisId: 分析记录ID
*/
function deleteAnalysis(analysisId) {
    return request('post', `${API_PATHS.EXAM_ANALYSIS}/delete/${analysisId}`);
}

/*
* 删除考试分析（逻辑删除）
* 参数analysisId: 分析记录ID
*/
function softDeleteAnalysis(analysisId) {
    return request('post', `${API_PATHS.EXAM_ANALYSIS}/soft-delete/${analysisId}`);
}

/*
* 分页查询用户的考试分析
* 参数params需包含：
*   page: 页码
*   pageSize: 每页条数
*/
function getAnalysisList(params) {
    return request('get', `${API_PATHS.EXAM_ANALYSIS}/list-by-user`, params);
}

/*
* 获取分析详情
* 参数analysisId: 分析记录ID
*/
function getAnalysisDetail(analysisId) {
    return request('get', `${API_PATHS.EXAM_ANALYSIS}/detail/${analysisId}`);
}

/*
* 考试记录接口  
​**​*​**​*​**​*​**​*​**​*​**​*​**​*​**​*​**​*​**​*​**​*​**​*​**​**/
/**
 * 获取试卷统计列表
 * @returns 试卷状态列表
 */
function getPapersState(params) {    
    return request('get', `${API_PATHS.EXAM_RECORD}/list-paper-state`,params);
}

/*
* 创建考试记录
* 参数recordData: 考试记录数据对象
*/
function createExamRecord(recordData) {
    return request('post', `${API_PATHS.EXAM_RECORD}/create`, recordData);
}

/*
* 批量创建考试记录
* 参数records: 考试记录数据数组
*/
function batchCreateExamRecords(records) {
    return request('post', `${API_PATHS.EXAM_RECORD}/batch-create`, records);
}

/*
* 更新考试记录
* 参数recordData: 更新后的记录数据
*/
function updateExamRecord(recordData) {
    return request('post', `${API_PATHS.EXAM_RECORD}/update`, recordData);
}

/*
* 删除考试记录（物理删除）
* 参数recordId: 记录ID
*/
function deleteExamRecord(recordId) {
    return request('post', `${API_PATHS.EXAM_RECORD}/delete/${recordId}`);
}

/*
* 删除考试记录（逻辑删除）
* 参数recordId: 记录ID
*/
function softDeleteExamRecord(recordId) {
    return request('post', `${API_PATHS.EXAM_RECORD}/soft-delete/${recordId}`);
}

/*
* 提交考试成绩
* 参数params需包含：
*   recordId: 记录ID
*   score: 分数
*/
function submitExamResult(params) {
    return request('post', `${API_PATHS.EXAM_RECORD}/submit-result`, params);
}

/*
* 分页查询用户的考试记录
* 参数params需包含：
*   page: 页码
*   pageSize: 每页条数
*/
function getExamRecordList(params) {
    return request('get', `${API_PATHS.EXAM_RECORD}/list-by-user`, params);
}

/*
* 查询某场考试的所有记录
* 参数params需包含：
*   examId: 考试ID
*   page: 页码
*   pageSize: 每页条数
*/
function getRecordsByExam(params) {
    return request('get', `${API_PATHS.EXAM_RECORD}/list-by-exam`, params);
}

/*
* 获取考试记录详情
* 参数recordId: 记录ID
*/
function getExamRecordDetail(recordId) {
    return request('get', `${API_PATHS.EXAM_RECORD}/detail/${recordId}`);
}


/*
* 使用考试id获取所有的考上记录
* 参数examId: 考试ID
*/
function getExamRecordByExamId(exam) {
    return request('get', `${API_PATHS.EXAM_RECORD}/list-by-exam-id`,exam);
}

// 统一导出所有接口和路径常量
export {
    API_PATHS,
    submitAnswer, batchSubmitAnswers, getAnswerById, getAnswersByExamRecord,
    updateAnswer, deleteAnswer, deleteAnswersByRecord, finishExam, submitExam,
    createAnalysis, batchCreateAnalyses, updateAnalysis, deleteAnalysis,
    softDeleteAnalysis, getAnalysisList, getAnalysisDetail, createExamRecord,
    batchCreateExamRecords, updateExamRecord, deleteExamRecord, softDeleteExamRecord,
    submitExamResult, getExamRecordList, getRecordsByExam, getExamRecordDetail, getPapersState,
    updateExam,getExamRecordByExamId, analysisThisExam, getTotalAnalysisByExamId
};